package cn.tedu.jsd2210.csmall.product.web;

import cn.tedu.jsd2210.csmall.product.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import java.io.Serializable;

@Data
//@NoArgsConstructor
//@AllArgsConstructor
//@JsonInclude(JsonInclude.Include.ALWAYS)
public class JsonResult<T> implements Serializable {
    @ApiModelProperty("业务状态码")
    private Integer state;
//    @JsonInclude(JsonInclude.Include.NON_NULL)//范围小的优先
    @ApiModelProperty("提示文本")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;

//    public static <Void>JsonResult ok(){//?
    public static JsonResult<Void> ok(){
//        JsonResult jsonResult=new JsonResult();
//        jsonResult.state=ServiceCode.OK.getValue();
//        return jsonResult;
        return ok(null);//不成文的规定,用参数少的方法调用参数多的方法,代码复用.
    }

    public static <T>JsonResult<T> ok(T data){
        JsonResult<T> jsonResult=new JsonResult();
        jsonResult.state=ServiceCode.OK.getValue();
        jsonResult.data=data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
//        JsonResult jsonResult=new JsonResult();
//        jsonResult.state=serviceCode.getValue();
//        jsonResult.message=message;
//        return jsonResult;
        return fail(e.getCode(),e.getMessage() );
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode ,String message){
        JsonResult<Void> jsonResult=new JsonResult();
        jsonResult.state=serviceCode.getValue();
        jsonResult.message=message;
        return jsonResult;
    }
}
